// 家居物品管理系统模拟数据
// 包含各种家居物品、位置和分类的模拟数据

const categories = [
    { id: 1, name: '基础图标', icon: 'box' },
    { id: 2, name: '数码', icon: 'computer' },
    { id: 3, name: '首饰', icon: 'ring' },
    { id: 4, name: '美妆', icon: 'lipstick' },
    { id: 5, name: '厨具', icon: 'knife' },
    { id: 6, name: '工具', icon: 'hammer' },
    { id: 7, name: '玩具', icon: 'blocks' },
    { id: 8, name: '运动', icon: 'basketball' },
    { id: 9, name: '家电', icon: 'refrigerator' },
    { id: 10, name: '交通', icon: 'car' },
    { id: 11, name: '服饰', icon: 'clothes' },
];

const locations = [
    { id: 1, name: '客厅', icon: 'living-room' },
    { id: 2, name: '主卧', icon: 'master-bedroom' },
    { id: 3, name: '次卧', icon: 'bedroom' },
    { id: 4, name: '厨房', icon: 'kitchen' },
    { id: 5, name: '卫生间', icon: 'bathroom' },
    { id: 6, name: '书房', icon: 'study' },
    { id: 7, name: '阳台', icon: 'balcony' },
    { id: 8, name: '餐厅', icon: 'dining' },
    { id: 9, name: '储物间', icon: 'storage-room' },
    { id: 10, name: '车库', icon: 'garage' }
];

const statuses = [
    { id: 1, name: '正常使用', value: 'active' },
    { id: 2, name: '需要维修', value: 'maintenance' },
    { id: 3, name: '已损坏', value: 'damaged' },
    { id: 4, name: '已丢失', value: 'lost' },
    { id: 5, name: '已处理', value: 'disposed' }
];

// 生成随机日期
const getRandomDate = (start, end) => {
    return new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime()));
};

// 生成随机价格
const getRandomPrice = (min, max) => {
    return parseFloat((Math.random() * (max - min) + min).toFixed(2));
};

// 生成随机ID
const generateId = () => {
    return Math.floor(Math.random() * 10000) + 1;
};

// 生成物品列表
const generateItems = (count) => {
    const items = [];
    const today = new Date();
    const twoYearsAgo = new Date();
    twoYearsAgo.setFullYear(today.getFullYear() - 2);

    const itemNames = [
        { name: '沙发', categoryId: 1 },
        { name: '茶几', categoryId: 1 },
        { name: '电视柜', categoryId: 1 },
        { name: '餐桌', categoryId: 1 },
        { name: '餐椅', categoryId: 1 },
        { name: '床', categoryId: 1 },
        { name: '衣柜', categoryId: 1 },
        { name: '书桌', categoryId: 1 },
        { name: '书架', categoryId: 1 },
        { name: '电视机', categoryId: 2 },
        { name: '冰箱', categoryId: 2 },
        { name: '洗衣机', categoryId: 2 },
        { name: '空调', categoryId: 2 },
        { name: '微波炉', categoryId: 2 },
        { name: '电饭煲', categoryId: 2 },
        { name: '电水壶', categoryId: 2 },
        { name: '吹风机', categoryId: 2 },
        { name: '电熨斗', categoryId: 2 },
        { name: '锅', categoryId: 3 },
        { name: '碗', categoryId: 3 },
        { name: '盘子', categoryId: 3 },
        { name: '筷子', categoryId: 3 },
        { name: '勺子', categoryId: 3 },
        { name: '菜刀', categoryId: 3 },
        { name: '砧板', categoryId: 3 },
        { name: '保鲜盒', categoryId: 3 },
        { name: '毛巾', categoryId: 4 },
        { name: '浴巾', categoryId: 4 },
        { name: '牙刷', categoryId: 4 },
        { name: '牙膏', categoryId: 4 },
        { name: '香皂', categoryId: 4 },
        { name: '沐浴露', categoryId: 4 },
        { name: '洗发水', categoryId: 4 },
        { name: '画框', categoryId: 5 },
        { name: '花瓶', categoryId: 5 },
        { name: '装饰画', categoryId: 5 },
        { name: '摆件', categoryId: 5 },
        { name: '地毯', categoryId: 5 },
        { name: '窗帘', categoryId: 5 },
        { name: '收纳盒', categoryId: 6 },
        { name: '收纳篮', categoryId: 6 },
        { name: '挂钩', categoryId: 6 },
        { name: '置物架', categoryId: 6 },
        { name: '衣架', categoryId: 6 },
        { name: '被子', categoryId: 7 },
        { name: '枕头', categoryId: 7 },
        { name: '床单', categoryId: 7 },
        { name: '枕套', categoryId: 7 },
        { name: '被套', categoryId: 7 },
        { name: '扫把', categoryId: 8 },
        { name: '拖把', categoryId: 8 },
        { name: '抹布', categoryId: 8 },
        { name: '垃圾桶', categoryId: 8 },
        { name: '洗衣液', categoryId: 8 },
        { name: '螺丝刀', categoryId: 9 },
        { name: '锤子', categoryId: 9 },
        { name: '扳手', categoryId: 9 },
        { name: '测量尺', categoryId: 9 },
        { name: '手电筒', categoryId: 9 }
    ];

    const brands = [
        '宜家', '无印良品', '欧普照明', '海尔', '美的', '格力',
        '苹果', '华为', '三星', '小米', '戴森', '飞利浦', '西门子',
        '博世', '松下', '索尼', '三菱', '虎牌', '双立人', '乐扣',
        '泰福高', '康宁', '美厨', '龙的', '九阳', '夏普', '大宇',
        '中顺洁柔', '立白', '蓝月亮', '佳能', '尼康', '奥林巴斯'
    ];

    const specifications = [
        '小号', '中号', '大号', '特大号', '迷你型', '标准型', '豪华型',
        '单人', '双人', '三人', '多人', '儿童', '成人', '老人',
        '1.2m', '1.5m', '1.8m', '2.0m', '简约型', '复古型', '现代型',
        '白色', '黑色', '灰色', '米色', '红色', '蓝色', '绿色', '黄色',
        '木质', '金属', '塑料', '玻璃', '陶瓷', '布艺', '皮质',
        '电动', '手动', '自动', '半自动', '智能', '普通', '高端'
    ];

    // 生成随机物品
    for (let i = 0; i < count; i++) {
        const randomItem = itemNames[Math.floor(Math.random() * itemNames.length)];
        const randomBrand = brands[Math.floor(Math.random() * brands.length)];
        const randomSpec = specifications[Math.floor(Math.random() * specifications.length)];
        const randomLocation = locations[Math.floor(Math.random() * locations.length)].id;
        const randomStatus = statuses[Math.floor(Math.random() * statuses.length)].value;

        items.push({
            id: generateId(),
            name: randomItem.name,
            categoryId: randomItem.categoryId,
            category: categories.find(c => c.id === randomItem.categoryId).name,
            brand: randomBrand,
            specification: randomSpec,
            price: getRandomPrice(50, 5000),
            purchaseDate: getRandomDate(twoYearsAgo, today).toISOString().split('T')[0],
            locationId: randomLocation,
            location: locations.find(l => l.id === randomLocation).name,
            status: randomStatus,
            warranty: Math.floor(Math.random() * 24) + 1,
            notes: Math.random() > 0.7 ? `这是一个${randomItem.name}，购买于${randomBrand}专卖店` : '',
            image: Math.random() > 0.5 ? `https://picsum.photos/id/${Math.floor(Math.random() * 1000)}/200/200` : ''
        });
    }

    return items;
};

// 导出数据
export const mockCategories = categories;
export const mockLocations = locations;
export const mockStatuses = statuses;
export const mockItems = generateItems(100);

// 导出获取数据的函数
export const getItems = (params = {}) => {
    let result = [...mockItems];

    // 筛选
    if (params.categoryId) {
        result = result.filter(item => item.categoryId === parseInt(params.categoryId));
    }

    if (params.locationId) {
        result = result.filter(item => item.locationId === parseInt(params.locationId));
    }

    if (params.status) {
        result = result.filter(item => item.status === params.status);
    }

    if (params.search) {
        const searchLower = params.search.toLowerCase();
        result = result.filter(item =>
            item.name.toLowerCase().includes(searchLower) ||
            item.brand.toLowerCase().includes(searchLower) ||
            item.specification.toLowerCase().includes(searchLower)
        );
    }

    // 排序
    if (params.sortBy) {
        const sortOrder = params.sortOrder === 'desc' ? -1 : 1;
        result.sort((a, b) => {
            if (params.sortBy === 'price') {
                return (a.price - b.price) * sortOrder;
            } else if (params.sortBy === 'purchaseDate') {
                return (new Date(a.purchaseDate) - new Date(b.purchaseDate)) * sortOrder;
            } else {
                return a[params.sortBy].localeCompare(b[params.sortBy]) * sortOrder;
            }
        });
    }

    // 分页
    const page = params.page || 1;
    const pageSize = params.pageSize || 12; // 默认每页12条
    const total = result.length;
    const pageTotal = Math.ceil(total / pageSize);
    const start = (page - 1) * pageSize;
    const end = start + pageSize;

    return {
        items: result.slice(start, end),
        pagination: {
            total,
            pageSize,
            currentPage: page,
            totalPages: pageTotal
        }
    };
};

// 获取单个物品
export const getItemById = (id) => {
    const idToFind = typeof id === 'string' && id.startsWith('ls') || id.startsWith('ex') || id.startsWith('xp')
        ? id
        : parseInt(id);
    return mockItems.find(item => item.id === idToFind);
}; 